package fileIO;

import java.awt.BorderLayout;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import java.io.FileInputStream;
import java.io.InputStreamReader;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Timer;
import java.util.TimerTask;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;

public class NotepadFrame extends JFrame {
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;

	private static boolean jishi = false;

	private JTextArea text = new JTextArea();

	private JTextArea timer = new JTextArea();

	private JTextArea daojishi = new JTextArea();

	private JScrollPane scroll = new JScrollPane(text);

	private File file = new File("/home/tarena/Desktop/1.txt");;

	private long time = file.lastModified();

	public NotepadFrame() throws Exception {
		text.setLineWrap(true);
		JPanel j1 = new JPanel(new BorderLayout());
		JPanel j2 = new JPanel(new BorderLayout());
		JPanel j3 = new JPanel(new BorderLayout());
		add(j1);
		j1.add(timer, BorderLayout.NORTH);
		j1.add(j2, BorderLayout.CENTER);
		j2.add(j3, BorderLayout.NORTH);
		j3.add(daojishi, BorderLayout.WEST);
		JButton go = new JButton("倒计时开始");
		go.addActionListener(new ActionListener() {

			public void actionPerformed(ActionEvent e) {
				daojishi();
			}

		});
		j3.add(go, BorderLayout.EAST);
		j2.add(scroll, BorderLayout.CENTER);
		setSize(300, 200);
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		readIn();
		watchFile();
		showTime();

	}

	void readIn() throws Exception {
		FileInputStream in = new FileInputStream(file);
		text.read(new InputStreamReader(in), null);
		in.close();
	}

	private void daojishi() {
		jishi = !jishi;
		new Thread() {
			@Override
			public void run() {
				int inputtime;

				int min = 60 * 60;
				SimpleDateFormat sdf = new SimpleDateFormat("HH:mm:ss");
				while (jishi) {
					Date d = new Date(min * 1000 - 8 * 60 * 60 * 1000);
					String s = sdf.format(d);
					min--;
					try {
						Thread.sleep(1000);
					} catch (InterruptedException e) {
						e.printStackTrace();
					}
					daojishi.setText(s);
					if (s.equals("00:00:00")) {
						JOptionPane.showMessageDialog(null, "时间到了");
						jishi = !jishi;
						break;
					}
				}
			}
		}.start();
	}

	private void showTime() {
		final TimerTask task = new TimerTask() {
			public void run() {
				Date d = new Date();
				SimpleDateFormat sdf = new SimpleDateFormat("HH:mm:ss");
				String s = sdf.format(d);
				timer.setText(s);
			}
		};
		Thread showtime = new Thread() {
			public void run() {
				Timer t = new Timer();
				t.schedule(task, 0, 1000);
			}
		};
		showtime.start();

	}

	public void watchFile() {
		Thread watchfile = new Thread() {
			public void run() {
				while (true) {
					long t = file.lastModified();
					if (t != time) {
						try {
							readIn();
						} catch (Exception e) {
							e.printStackTrace();
						}
						time = t;
					}
					try {
						Thread.sleep(10000);
					} catch (InterruptedException e) {
						e.printStackTrace();
					}
				}
			}
		};
		// 低优先级，后台程序
		watchfile.setPriority(4);
		watchfile.setDaemon(true);
		watchfile.start();
	}

	public static void main(String[] args) throws Exception {
		new NotepadFrame().setVisible(true);
	}
}
